package com.example.yogurtsystem.controller;

import com.example.yogurtsystem.dto.ResultDTO;
import com.example.yogurtsystem.dto.UserDto;
import com.example.yogurtsystem.dto.ValidateDto;
import com.example.yogurtsystem.entity.User;
import com.example.yogurtsystem.service.UserService;
import com.example.yogurtsystem.utils.TableTagBean;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

/**
 * @author ZXB
 * @version 1.0.0
 * @ClassName UserController
 * @Description 用户控制器
 * @Date 2021/8/11 23:39
 */
@RestController
@RequestMapping("/user/")
@Validated
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户列表")
    @GetMapping("/listUser")
    public ResultDTO<?> listUser() {
        return ResultDTO.requestSuccess(userService.listUser());
    }

    @ApiOperation("增加用户")
    @PostMapping("/insertUser")
    public ResultDTO<?> saveUser(@Validated(value = {ValidateDto.save.class}) @RequestBody UserDto userDto) {
        userService.saveUser(userDto);
        return ResultDTO.requestSuccess("新增用户成功");
    }

    @ApiOperation("删除用户")
    @PostMapping("/deleteUser")
    public ResultDTO<?> deleteUser(@NotBlank(message = "id不能为空") @RequestParam(value = "id", required = false) String id) {
        userService.deleteUser(id);
        return ResultDTO.requestSuccess("删除用户成功");
    }

    @ApiOperation("修改用户")
    @PostMapping("/updateUser")
    public ResultDTO updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return ResultDTO.requestSuccess("更新用户成功");
    }

//    @ApiOperation("分页查询用户列表")
//    @GetMapping("/selectUserByLimit")
//    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "pageNum", value = "起始页数", required = true, dataType = "String"),
//            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页记录数", required = true, dataType = "String")})
//    public ResultDTO selectUserByLimit(HttpServletRequest request) {
//        TableTagBean ttb=TableTagBean.getFromExt(request);
//        return ResultDTO.requestSuccess(userService.selectUserByLimit(ttb));
//    }

    @ApiOperation("分页查询用户列表")
    @GetMapping("/selectUserByLimit")
    public ResultDTO selectUserByLimit(int pageNum, int pageSize, String name) {
        return userService.selectUserByLimit(pageNum, pageSize, name);
    }

    @PostMapping("/import")
    public ResultDTO addSubject(MultipartFile file) {
        userService.importUserData(file, userService);
        return ResultDTO.requestSuccess();
    }
}
